//
//  MyMainTabBar.swift
//  weibo08
//
//  Created by 陈杰明 on 16/2/27.
//  Copyright © 2016年 JM. All rights reserved.
//

import UIKit
//定义协议
@objc protocol MyMainTabBarDelegate: NSObjectProtocol{
    //表示协议可选
    optional
     func mainTabBarComposeClick()
    
}

class MyMainTabBar: UITabBar {
    //定义协议属性
    weak var composeDelegate: MyMainTabBarDelegate?
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //将composeButton的frame拉大
        let width = frame.width / 5
        composeButton.frame = CGRect(x: 0, y: 0, width: width + 5, height: frame.height)
        
        //设置中心点
        composeButton.center = CGPoint(x: frame.width / 2, y: frame.height / 2)
//        print("\(composeButton)")
    }
    
    //MARK -按钮点击事件
    func btnDidClick(button: UIButton) {
        print("按钮被点击了")
        //调用协议
        self.composeDelegate?.mainTabBarComposeClick?()
        
    }
    
    // MARK: -懒加载
    lazy var composeButton: UIButton = {
        let button = UIButton()
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //设置加号图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //自适应
        button.sizeToFit()
        
        //把按钮添加到tarBar
        self.addSubview(button)
        
        //添加点击事件
        button.addTarget(self, action: "btnDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
}
